var gtv = 0,ctg = 0;
var coin = 0,gift = 0;
var type = "";
$(function () {
    getExchangeConfig();
    $(".back-wrapper").click(function () {
        location.href = "./mine.html?channel="+getQueryString("channel");
    });
    $(".exchange-recode-btn").click(function () {
        location.href = "./exchangeRecord.html?channel="+getQueryString("channel");
    });
    $(".right-wrapper .btn").click(function () {
        if($(this).hasClass("disabled")){
            Toast("余额不足，请充值");
            return;
        }
        $(".num-wrapper").html(1);
        if($(this).attr("data-type") == "vip"){
            type = "vip";
            $(".get-wrapper").html(`<div class="fr">获得vip会员<span class="get-count">1</span>天</div>`);
            $(".cost-detail").html(`合计花费：<span class="cost-count">${gtv}</span>书券`);
            $(".confirm-btn").html("确认vip");
        }
        if($(this).attr("data-type") == "gift"){
            type = "gift";
            $(".get-wrapper").html(`<div class="fr">获得<span class="get-count">100</span>书券</div>`);
            $(".cost-detail").html(`合计花费：<span class="cost-count">${ctg}</span>书币`);
            $(".confirm-btn").html("确认");
        }
        $(".exchange-mask").show();
    });
    $(".close-wrapper span").click(function () {
        $(".exchange-mask").hide();
    });
    $(".add-num").click(function () {
        var num = parseInt($(".num-wrapper").html());
        $(".num-wrapper").html(num+1);
        if(type == "vip"){
            $(".get-count").html(num+1);
            $(".cost-count").html((num+1)*gtv);
        }
        if(type == "gift"){
            $(".get-count").html((num+1)*100);
            $(".cost-count").html((num+1)*ctg);
        }
    });
    $(".decrease-num").click(function () {
        var num = parseInt($(".num-wrapper").html());
        if(num == 1){
            Toast("数量不能小于1");
            return;
        }
        $(".num-wrapper").html(num-1);
        if(type == "vip"){
            $(".get-count").html(num-1);
            $(".cost-count").html((num-1)*gtv);
        }
        if(type == "gift"){
            $(".get-count").html((num-1)*100);
            $(".cost-count").html((num-1)*ctg);
        }
    });
    $(".confirm-btn").click(function () {
        var amount = parseInt($(".num-wrapper").html());
        if(type == "vip"){
            var need = parseInt($(".cost-count").html());
            if(need > gift){
                Toast("余额不足，请充值");
                return;
            }
            doExchange("vip",amount);
        }
        if(type == "gift"){
            var need = parseInt($(".cost-count").html());
            if(need > coin){
                Toast("余额不足，请充值");
                return;
            }
            doExchange("gift",amount);
        }
    });
});

function getExchangeConfig() {
    $.ajax({
        type:'get',
        url:BASE_URl + "config/exchange",
        success:function (data) {
            if(data.success){
                gtv = data.data.gtv;
                ctg = data.data.ctg;
                coin = data.data.coin;
                gift = data.data.gift;
                DrawExchangeConfig(data.data);
            }
        }
    });
}

function DrawExchangeConfig(data) {
    $(".coin-wrapper").html(data.coin);
    $(".gift-wrapper").html(data.gift);
    $(".exchange-vip-wrapper .amount span").html(data.gtv + "书券");
    $(".exchange-gift-wrapper .amount span").html(data.ctg + "书币");
    if(data.gift < data.gtv){
        $(".exchange-vip-wrapper .btn").addClass("disabled");
    }else{
        $(".exchange-vip-wrapper .btn").removeClass("disabled");
    }
    if(data.coin < data.ctg){
        $(".exchange-gift-wrapper .btn").addClass("disabled");
    }else{
        $(".exchange-gift-wrapper .btn").removeClass("disabled");
    }
    console.log(data);
}

function doExchange(prop,amount) {
    $.ajax({
        type:'get',
        url:BASE_URl + "user/exchange?prod="+prop+"&amount="+amount,
        success:function (data) {
            if(data.success){
                coin = data.data.coin;
                gift = data.data.gift;
                $(".coin-wrapper").html(coin);
                $(".gift-wrapper").html(gift);
                if(gift < gtv){
                    $(".exchange-vip-wrapper .btn").addClass("disabled");
                }else{
                    $(".exchange-vip-wrapper .btn").removeClass("disabled");
                }
                if(coin < ctg){
                    $(".exchange-gift-wrapper .btn").addClass("disabled");
                }else{
                    $(".exchange-gift-wrapper .btn").removeClass("disabled");
                }
                $(".exchange-mask").hide();
                Toast("兑换成功");
            }
        }
    });
}